import smCrypto from 'sm-crypto'
import bytesString from './bytesString.js'


// ua信息
const ua = navigator ? navigator.userAgent.toLowerCase() : ''

export default {
	// 保存token
	saveToken(token) {
		try {
			uni.setStorageSync('cloud-water-user-token', token)
		} catch (error) {
			console.log('saveToken', error)
		}
	},
	// 获取token
	getToken() {
		try {
			return uni.getStorageSync('cloud-water-user-token')
		} catch (error) {
			console.log('getToken', error)
			return null;
		}
	},
	// 保存用户信息
	saveUserInfo(userInfo) {
		try {
			uni.setStorageSync('cloud-water-user-info', userInfo)
		} catch (error) {
			console.log('saveUserInfo', error)
		}
	},
	// 获取用户信息
	getUserInfo() {
		try {
			return uni.getStorageSync('cloud-water-user-info')
		} catch (error) {
			console.log('getUserInfo', error)
			return null;
		}
	},
	// 获取url参数
	search(key) {
		// #ifdef H5
		let query = window.location.search.substring(1);
		let vars = query.split('&');
		for (let i = 0; i < vars.length; i++) {
			const pair = vars[i].split('=');
			if (pair[0] === key) {
				return pair[1];
			}
		}
		// #endif
		return false;
	},
	// 加载js文件
	loaderJs(url) {
		let script = document.createElement('script');
		script.src = url;
		document.head.appendChild(script);
	},
	// 完美校园
	isWanxiao: ua.indexOf('wanxiao') != -1,
	// 超级App
	isSuperapp: ua.indexOf('superapp') != -1,
	// 支付宝
	isalipay: ua.indexOf('alipay') != -1,
	// 钉钉
	isDingding: ua.indexOf('dingtalk') != -1,
	// 今日校园
	isWisedu: ua.indexOf('wisedu') != -1,
}